package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.CargoInterno;

public class CargoInternoDaoImpl implements CargoInternoDao {
	Connection c;

	public CargoInternoDaoImpl() {
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}

	@Override
	public void salvaCargoInterno(CargoInterno cargoInterno)
			throws SQLException {
		String sql = "INSERT INTO cargosInternos VALUES (?,?)";
		PreparedStatement ps = c.prepareStatement(sql);

		ps.setInt(1, cargoInterno.getIdCargInt());
		ps.setString(2, cargoInterno.getNomeCargInt());

		ps.execute();
		ps.close();

	}

	@Override
	public void editaCargoInterno(CargoInterno cargoInterno)
			throws SQLException {
		String sql = "UPDATE cargosInternos SET nomeCargInt = ? WHERE idCargInt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, cargoInterno.getNomeCargInt());
		ps.setInt(2, cargoInterno.getIdCargInt());
		ps.execute();
		ps.close();

	}

	@Override
	public void excluiCargoInterno(CargoInterno cargoInterno)
			throws SQLException {
		String sql = "DELETE cargosInternos WHERE idCargInt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, cargoInterno.getIdCargInt());
		ps.execute();
		ps.close();

	}

	@Override
	public CargoInterno pesquisaCargoInterno(CargoInterno cargoInterno)
			throws SQLException {
		CargoInterno ci = new CargoInterno();
		String sql = "SELECT idCargInt, nomeCargInt FROM cargosInternos WHERE idCargInt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, cargoInterno.getIdCargInt());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			ci.setIdCargInt(rs.getInt("idCargInt"));
			ci.setNomeCargInt(rs.getString("nomeCargInt"));

		}
		ps.close();
		rs.close();
		return ci;
	}

}
